<template>
	<view class="">
		<u-cell-group>
			<u-cell-item title="版本更新" :value="version" @click="updata"></u-cell-item>
			<u-cell-item title="退出登录" :arrow="isNewVresion" @click="showCancel = true"></u-cell-item>
		</u-cell-group>
		<u-modal v-model="showCancel" :show-cancel-button="true" content="请确认是否要退出登录" @confirm="exitHandle"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showCancel: false,
				isNewVresion: false,
				version: '',
				curVersionInfo: {}
			}
		},
		mounted() {
			//<!-- #ifdef APP-PLUS -->
			this.version = plus.runtime.version
			this.isAndroid();
			//<!-- #endif -->
		},
		methods: {
			exitHandle() {
				uni.clearStorage();
				uni.reLaunch({
				    url: '/pages/login/index'
				});
			},
			isAndroid() {
				var _this = this;
				uni.getSystemInfo({
					success: (res) => {
						console.log(res)
						if (res.platform == 'android') {
							_this.$u.http.config.header.platform = res.platform;
							_this.$u.api.version({
								appType: 'KB-MERCHANT-UNDERLYING',
								versionName: plus.runtime.version
							}).then(res => {
								if (res.code == 0) {
									this.isNewVresion = true;
									this.curVersionInfo = res.data;
									this.version = this.curVersionInfo.versionName;
									
								} else {
									this.isNewVresion = false;
								}
							})
						}
			
					}
				})
			},
			updata() {
				let _this = this;
				uni.showModal({
					title: '发现新版本' + this.curVersionInfo.versionName,
					content: this.curVersionInfo.updateLog,
					success: (result) => {
						if (result.confirm == true) {
							_this.downLoad(this.curVersionInfo.downloadUrl);
						}
					}
				})
			},
			downLoad(url) {
				uni.showLoading({
					title: '更新中……'
				});
				console.log(url)
				uni.downloadFile({
				    url: url, //仅为示例，并非真实的资源
				    success: (res) => {
				        if (res.statusCode === 200) {
							uni.hideLoading();
							uni.showModal({
								title:'',
								content:'更新成功，确定现在重启吗？',
								confirmText: '重启',
								confirmColor: '#EE8F57',
								success: function(result) {
									if (result.confirm == true) {
										plus.runtime.install(res.tempFilePath,{
											force:true
										},function(){
											utils.showToast('更新成功，重启中');
											plus.runtime.restart();
										})
									}
								}
							})
				        }
				    }
				});
			},
		}
	}
</script>

<style>
</style>
